package com.obas.project.view {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.constants.TestingLibConstants;
   import com.obas.event.DataRefreshEvent;
   import com.obas.event.ItestOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.DataRefreshHelper;
   import com.obas.model.business.SystemProject;
   import com.ease.component.constant.LoadingStateName;
   import com.ease.component.managers.ErrorManager;

   import flash.events.EventDispatcher;
   import flash.events.MouseEvent;

   import mx.collections.ArrayCollection;
   import mx.managers.PopUpManager;
   import mx.validators.Validator;

   [Event(name="{com.obas.event.ItestOperationEvent.REQUEST_ID}", type="com.obas.event.ItestOperationEvent")]
   [Event(name="{com.obas.event.DataRefreshEvent.REQUEST_ID}", type="com.obas.event.DataRefreshEvent")]
   public class ProjectNewViewMediator extends EventDispatcher {

      public var _view:ProjectNewView;

      [InjectableView]
      public function get view():ProjectNewView {
         return _view;
      }

      public function set view(value:ProjectNewView):void {
         _view = value;
         init();
      }

      private function init():void {
         view.cancelButton.addEventListener(MouseEvent.CLICK, onCancel);
         view.okButton.addEventListener(MouseEvent.CLICK, onOk);

         if(_view.actionType == TestingLibConstants.ACTION_UPDATE) {
            _view.title = "修改项目";
         }
      }

      private function onCancel(e:* = null):void {
         PopUpManager.removePopUp(view);
      }

      private function onOk(e:*):void {
         var vrets:Array = Validator.validateAll(view.validators);

         if(vrets && vrets.length > 0) {
            return;
         }

         if(_view.actionType == TestingLibConstants.ACTION_CREATE) {
            var sysProject:SystemProject = new SystemProject();
            sysProject.name = view.projectName.text;
            view.loadingState = LoadingStateName.LOADING;
            dispatchEvent(new ItestOperationEvent([sysProject], ItestOperationEvent.ADD_PROJECT));
         }
         else if(_view.actionType == TestingLibConstants.ACTION_UPDATE) {
            var systemProject:SystemProject = _view.systemProject;
            if(systemProject.name == _view.projectName.text) {
               return;
            }
            systemProject.name = _view.projectName.text;
            dispatchEvent(new ItestOperationEvent([systemProject], ItestOperationEvent.UPDATE_PROJECT));
         }

      }

      private function checkSystemProjectNameIsValid():Boolean {

         return true;
      }

      [ResponseHandler("{com.obas.event.ItestOperationEvent.RETURN_ID}")]
      public function onTaskOperationEvent(event:CommandCompletionEvent):void {
         view.loadingState = LoadingStateName.LOADED;
         if (event.error) {
            ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
            return ;
         }
         var request:ItestOperationEvent = event.request as ItestOperationEvent;
         switch (request.actionType) {
            case ItestOperationEvent.ADD_PROJECT:
            case ItestOperationEvent.UPDATE_PROJECT:
               var sysProject:SystemProject = event.result as SystemProject;
               var helper:DataRefreshHelper = new DataRefreshHelper();
               var lists:ArrayCollection = new ArrayCollection;
               lists.addItem(sysProject);
               helper.data = lists;
               dispatchEvent(new DataRefreshEvent(helper, DataRefreshEvent.TYPE_ADD_UPDATE));
               onCancel();
               break;
         }
      }
   }
}